home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / includes / router.php < prev    next >
PHP Script  |  2008-07-06  |  9KB  |  433 lines

  1. <?php
  2. /**
  3. * @version        $Id: router.php 8180 2007-07-23 05:52:29Z eddieajau $
  4. * @package        Joomla.Framework
  5. * @subpackage    Application
  6. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license        GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE') or die();
  17.  
  18. /**
  19.  * Class to create and parse routes for the site application
  20.  *
  21.  * @author        Johan Janssens <johan.janssens@joomla.org>
  22.  * @package     Joomla
  23.  * @since        1.5
  24.  */
  25. class JRouterSite extends JRouter
  26. {
  27.     /**
  28.      * Class constructor
  29.      *
  30.      * @access public
  31.      */
  32.     function __construct($options = array()) {
  33.         parent::__construct($options);
  34.     }
  35.  
  36.     function parse(&$uri)
  37.     {
  38.         $vars = array();
  39.  
  40.         // Get the path
  41.         $path = $uri->getPath();
  42.  
  43.         //Remove the suffix
  44.         if($this->_mode == JROUTER_MODE_SEF)
  45.         {
  46.             // Get the application
  47.             $app =& JFactory::getApplication();
  48.  
  49.             if($app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/'))
  50.             {
  51.                 if($suffix = pathinfo($path, PATHINFO_EXTENSION))
  52.                 {
  53.                     $path = str_replace('.'.$suffix, '', $path);
  54.                     $vars['format'] = $suffix;
  55.                 }
  56.             }
  57.         }
  58.  
  59.         //Remove basepath
  60.         $path = substr_replace($path, '', 0, strlen(JURI::base(true)));
  61.  
  62.         //Remove prefix
  63.         $path = str_replace('index.php', '', $path);
  64.  
  65.         //Set the route
  66.         $uri->setPath(trim($path , '/'));
  67.  
  68.         $vars += parent::parse($uri);
  69.  
  70.         return $vars;
  71.     }
  72.  
  73.     function &build($url)
  74.     {
  75.         $uri =& parent::build($url);
  76.  
  77.         // Get the path data
  78.         $route = $uri->getPath();
  79.  
  80.         //Add the suffix to the uri
  81.         if($this->_mode == JROUTER_MODE_SEF && $route)
  82.         {
  83.             $app =& JFactory::getApplication();
  84.  
  85.             if($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/'))
  86.             {
  87.                 if($format = $uri->getVar('format', 'html'))
  88.                 {
  89.                     $route .= '.'.$format;
  90.                     $uri->delVar('format');
  91.                 }
  92.             }
  93.  
  94.             if($app->getCfg('sef_rewrite'))
  95.             {
  96.                 //Transform the route
  97.                 $route = str_replace('index.php/', '', $route);
  98.             }
  99.         }
  100.  
  101.         //Add basepath to the uri
  102.         $uri->setPath(JURI::base(true).'/'.$route);
  103.  
  104.         return $uri;
  105.     }
  106.  
  107.     function _parseRawRoute(&$uri)
  108.     {
  109.         $vars   = array();
  110.  
  111.         $menu =& JSite::getMenu(true);
  112.  
  113.         //Handle an empty URL (special case)
  114.         if(!$uri->getVar('Itemid') && !$uri->getVar('option'))
  115.         {
  116.             $item = $menu->getDefault();
  117.             if(!is_object($item)) return $vars; // No default item set
  118.  
  119.             //Set the information in the request
  120.             $vars = $item->query;
  121.  
  122.             //Get the itemid
  123.             $vars['Itemid'] = $item->id;
  124.  
  125.             // Set the active menu item
  126.             $menu->setActive($vars['Itemid']);
  127.  
  128.             return $vars;
  129.         }
  130.  
  131.         //Get the variables from the uri
  132.         $this->setVars($uri->getQuery(true));
  133.  
  134.         //Get the itemid, if it hasn't been set force it to null
  135.         $this->setVar('Itemid', JRequest::getInt('Itemid', null));
  136.  
  137.         //Only an Itemid ? Get the full information from the itemid
  138.         if(count($this->getVars()) == 1)
  139.         {
  140.             $item = $menu->getItem($this->getVar('Itemid'));
  141.             $vars = $vars + $item->query;
  142.         }
  143.  
  144.         // Set the active menu item
  145.         $menu->setActive($this->getVar('Itemid'));
  146.  
  147.         return $vars;
  148.     }
  149.  
  150.     function _parseSefRoute(&$uri)
  151.     {
  152.         $vars   = array();
  153.  
  154.         $menu  =& JSite::getMenu(true);
  155.         $route = $uri->getPath();
  156.  
  157.         //Get the variables from the uri
  158.         $vars = $uri->getQuery(true);
  159.  
  160.         //Handle an empty URL (special case)
  161.         if(empty($route))
  162.         {
  163.  
  164.             //If route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately
  165.             if(isset($vars['option']) || isset($vars['Itemid'])) {
  166.                 return $this->_parseRawRoute($uri);
  167.             }
  168.  
  169.             $item = $menu->getDefault();
  170.  
  171.             //Set the information in the request
  172.             $vars = $item->query;
  173.  
  174.             //Get the itemid
  175.             $vars['Itemid'] = $item->id;
  176.  
  177.             // Set the active menu item
  178.             $menu->setActive($vars['Itemid']);
  179.  
  180.             return $vars;
  181.         }
  182.  
  183.  
  184.         /*
  185.          * Parse the application route
  186.          */
  187.  
  188.         if(substr($route, 0, 9) == 'component')
  189.         {
  190.             $segments    = explode('/', $route);
  191.             $route      = str_replace('component/'.$segments[1], '', $route);
  192.  
  193.             $vars['option'] = 'com_'.$segments[1];
  194.             $vars['Itemid'] = null;
  195.         }
  196.         else
  197.         {
  198.             //Need to reverse the array (highest sublevels first)
  199.             $items = array_reverse($menu->getMenu());
  200.  
  201.             foreach ($items as $item)
  202.             {
  203.                 $lenght = strlen($item->route); //get the lenght of the route
  204.  
  205.                 if($lenght > 0 && strpos($route.'/', $item->route.'/') === 0 && $item->type != 'menulink')
  206.                 {
  207.                     $route   = substr($route, $lenght);
  208.  
  209.                     $vars['Itemid'] = $item->id;
  210.                     $vars['option'] = $item->component;
  211.                     break;
  212.                 }
  213.             }
  214.         }
  215.  
  216.         // Set the active menu item
  217.         if ( isset($vars['Itemid']) ) {
  218.             $menu->setActive(  $vars['Itemid'] );
  219.         }
  220.  
  221.         //Set the variables
  222.         $this->setVars($vars);
  223.  
  224.         /*
  225.          * Parse the component route
  226.          */
  227.         if(!empty($route) && isset($this->_vars['option']) )
  228.         {
  229.             $segments = explode('/', $route);
  230.             array_shift($segments);
  231.  
  232.             // Handle component    route
  233.             $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $this->_vars['option']);
  234.  
  235.             // Use the component routing handler if it exists
  236.             $path = JPATH_BASE.DS.'components'.DS.$component.DS.'router.php';
  237.  
  238.             if (file_exists($path) && count($segments))
  239.             {
  240.                 if ($component != "com_search") { // Cheep fix on searches
  241.                     //decode the route segments
  242.                     $segments = $this->_decodeSegments($segments);
  243.                 }
  244.  
  245.                 require_once $path;
  246.                 $function =  substr($component, 4).'ParseRoute';
  247.                 $vars =  $function($segments);
  248.  
  249.                 $this->setVars($vars);
  250.             }
  251.         }
  252.         else
  253.         {
  254.             //Set active menu item
  255.             if($item =& $menu->getActive()) {
  256.                 $vars = $item->query;
  257.             }
  258.         }
  259.  
  260.         return $vars;
  261.     }
  262.  
  263.     function _buildRawRoute(&$uri)
  264.     {
  265.     }
  266.  
  267.     function _buildSefRoute(&$uri)
  268.     {
  269.         // Get the route
  270.         $route = $uri->getPath();
  271.  
  272.         // Get the query data
  273.         $query = $uri->getQuery(true);
  274.  
  275.         if(!isset($query['option'])) {
  276.             return;
  277.         }
  278.  
  279.         $menu =& JSite::getMenu();
  280.  
  281.         /*
  282.          * Build the component route
  283.          */
  284.         $component    = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']);
  285.         $tmp         = '';
  286.  
  287.         // Use the component routing handler if it exists
  288.         $path = JPATH_BASE.DS.'components'.DS.$component.DS.'router.php';
  289.  
  290.         // Use the custom routing handler if it exists
  291.         if (file_exists($path) && !empty($query))
  292.         {
  293.             require_once $path;
  294.             $function    = substr($component, 4).'BuildRoute';
  295.             $parts        = $function($query);
  296.  
  297.             // encode the route segments
  298.             if ($component != "com_search") { // Cheep fix on searches
  299.                 $parts = $this->_encodeSegments($parts);
  300.             }
  301.  
  302.             $result = implode('/', $parts);
  303.             $tmp    = ($result != "") ? '/'.$result : '';
  304.         }
  305.  
  306.         /*
  307.          * Build the application route
  308.          */
  309.         $built = false;
  310.         if (isset($query['Itemid']) && !empty($query['Itemid']))
  311.         {
  312.             $item = $menu->getItem($query['Itemid']);
  313.  
  314.             if (is_object($item) && $query['option'] == $item->component) {
  315.                 $tmp = !empty($tmp) ? $item->route.'/'.$tmp : $item->route;
  316.                 $built = true;
  317.             }
  318.         }
  319.  
  320.         if(!$built) {
  321.             $tmp = 'component/'.substr($query['option'], 4).'/'.$tmp;
  322.         }
  323.  
  324.         $route .= '/'.$tmp;
  325.  
  326.         // Unset unneeded query information
  327.         unset($query['Itemid']);
  328.         unset($query['option']);
  329.  
  330.         //Set query again in the URI
  331.         $uri->setQuery($query);
  332.         $uri->setPath($route);
  333.     }
  334.  
  335.     function _processParseRules(&$uri)
  336.     {
  337.         // Process the attached parse rules
  338.         $vars = parent::_processParseRules($uri);
  339.  
  340.         // Process the pagination support
  341.         if($this->_mode == JROUTER_MODE_SEF)
  342.         {
  343.             $app =& JFactory::getApplication();
  344.  
  345.             if($start = $uri->getVar('start'))
  346.             {
  347.                 $uri->delVar('start');
  348.                 $vars['limitstart'] = $start;
  349.             }
  350.         }
  351.  
  352.         return $vars;
  353.     }
  354.  
  355.     function _processBuildRules(&$uri)
  356.     {
  357.         // Make sure any menu vars are used if no others are specified
  358.         if(($this->_mode != JROUTER_MODE_SEF) && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2)
  359.         {
  360.             $menu =& JSite::getMenu();
  361.  
  362.             // Get the active menu item
  363.             $itemid = $uri->getVar('Itemid');
  364.             $item   = $menu->getItem($itemid);
  365.  
  366.             $uri->setQuery($item->query);
  367.             $uri->setVar('Itemid', $itemid);
  368.         }
  369.  
  370.         // Process the attached build rules
  371.         parent::_processBuildRules($uri);
  372.  
  373.         // Get the path data
  374.         $route = $uri->getPath();
  375.  
  376.         if($this->_mode == JROUTER_MODE_SEF && $route)
  377.         {
  378.             $app =& JFactory::getApplication();
  379.  
  380.             if ($limitstart = $uri->getVar('limitstart'))
  381.             {
  382.                 $uri->setVar('start', (int) $limitstart);
  383.                 $uri->delVar('limitstart');
  384.             }
  385.         }
  386.  
  387.         $uri->setPath($route);
  388.     }
  389.  
  390.     function &_createURI($url)
  391.     {
  392.         //Create the URI
  393.         $uri =& parent::_createURI($url);
  394.  
  395.         // Set URI defaults
  396.         $menu =& JSite::getMenu();
  397.  
  398.         // Get the itemid form the URI
  399.         $itemid = $uri->getVar('Itemid');
  400.  
  401.         if(is_null($itemid))
  402.         {
  403.             if($option = $uri->getVar('option'))
  404.             {
  405.                 $item  = $menu->getItem($this->getVar('Itemid'));
  406.                 if(isset($item) && $item->component == $option) {
  407.                     $uri->setVar('Itemid', $item->id);
  408.                 }
  409.             }
  410.             else
  411.             {
  412.                 if($option = $this->getVar('option')) {
  413.                     $uri->setVar('option', $option);
  414.                 }
  415.  
  416.                 if($itemid = $this->getVar('Itemid')) {
  417.                     $uri->setVar('Itemid', $itemid);
  418.                 }
  419.             }
  420.         }
  421.         else
  422.         {
  423.             if(!$uri->getVar('option'))
  424.             {
  425.                 $item  = $menu->getItem($itemid);
  426.                 $uri->setVar('option', $item->component);
  427.             }
  428.         }
  429.  
  430.         return $uri;
  431.     }
  432. }
  433.